from typing import List, Optional, Union
from models.common import OID
from pydantic import BaseModel
from fastapi import Query, Body, APIRouter

from components.router import CenterRoute
from ioservice.institution import get_institution, get_institution_by_id, search_institutions, aggregate_institution
from models.common import OID
from models.institution import InstitutionSearchModel
from controller.institution import get_institution_activities, get_institution_activities_alumni, \
    get_alumni_by_activities, get_target_active_institutions

router = APIRouter(route_class=CenterRoute)


@router.post("/institution/search")
async def api_search_institution(context: InstitutionSearchModel):
    res = await search_institutions(context)
    aggregation = await aggregate_institution(context)
    return {"items": res["data"], "total": res["total"], "aggregation": aggregation}


@router.get('/institution')
async def api_get_institution(name: Optional[str] = Query(None), province: Optional[str] = Query(None),
                              offset: int = Query(0, gte=0),
                              size: int = Query(20, le=100)):
    res = await get_institution(institution_name=name, province=province, skip=offset, limit=size)
    return res


@router.post('/institution/{institution_id}/info')
async def api_get_institution_by_id(institution_id: str,
                                    fields: Optional[List[str]] = Body(None, embed=True)):
    res = await get_institution_by_id(institution_id, projection=fields)
    return res


class InstitutionSearchParam(BaseModel):
    offset: Optional[int] = 0
    size: Optional[int] = 20


@router.post("/institution/{institution_id}/alumni")
async def api_get_institution_support_alumni(institution_id: OID, context: InstitutionSearchParam):
    """
    服务人才
    """
    total, data = await get_institution_activities_alumni(institution_id, offset=context.offset, size=context.size)
    return {"total": total, "data": data}


@router.post("/institution/{institution_id}/active/alumni")
async def api_get_institution_active_alumni(institution_id: OID, context: InstitutionSearchParam):
    """
    活跃人才
    """
    activities = await get_institution_activities(institution_id, sort=["-create_time"], size=1)
    if not activities:
        return {"total": 0, "data": []}
    activities_ids = [activity.id for activity in activities]
    total, data = await get_alumni_by_activities(activities_ids, offset=context.offset, size=context.size)
    return {"total": total, "data": data}


@router.post("/institution/{institution_id}/active/activities")
async def api_get_institution_active_activities(institution_id: OID, context: InstitutionSearchParam):
    """
        机构动态
    """
    total, data = await get_institution_activities(institution_id, offset=context.offset, size=context.size,
                                                   sort=["-start_time"])
    return {"total": total, "data": data}


@router.get("/institution/active/institution")
async def api_get_recent_active_institutions():
    """
        机构动态
    """
    res = await get_target_active_institutions()
    return res
